package net.sf.nlp.ctb;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public class CTBBracketedConvert 
{
	private static final String CTB_EXTENSION = ".ctb";
	private static final Pattern CTB_SEPARATOR_PATTERN = Pattern.compile("<.*>");
	protected static Logger logger =  Logger.getLogger(CTBBracketedConvert.class);
	
	
	public void convert(String srcPath, String destPath)
	{
		File filePath = new File(srcPath);
		
		if (!filePath.exists())
			logger.warn("The " + srcPath + " is not existed!");
		
		File[] files = filePath.listFiles();

		for (File file : files)
			convert(file, destPath);
		
		System.out.println("converting finished");
	}
	
	public void convert(File srcFile, String destPath)
	{
		System.out.println("converting " + srcFile.getName());
		String fileName = srcFile.getName();
		
		int index = fileName.indexOf(".");
		
		if (index != -1)
			fileName = fileName.substring(0, index);
		
		File destFile = new File(destPath + "/" + fileName + CTB_EXTENSION);
		
		StringBuffer bracketedTree = new StringBuffer();
		
		try
		{
			BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));
			BufferedReader reader = new BufferedReader(new FileReader(srcFile));
			
			String line = null;
			
			while ((line = reader.readLine()) != null)
			{
				if (line.trim().length() == 0 || CTB_SEPARATOR_PATTERN.matcher(line.trim()).matches() ||
					line.startsWith("( (") || line.startsWith("(("))
				{
					if (bracketedTree.length() > 0)
					{
						writer.write(bracketedTree.toString());
						writer.write("\r\n");
						writer.write("\r\n");
						writer.flush();
						bracketedTree.delete(0, bracketedTree.length());
					}
					
					if (line.startsWith("( (") || line.startsWith("(("))
					{
						bracketedTree.append(line).append("\r\n");
					}
				}
				else
				{
					bracketedTree.append(line).append("\r\n");
				}
			}
			
			if (bracketedTree.toString().trim().length() > 0)
			{
				writer.write(bracketedTree.toString());
				writer.write("\r\n");
				writer.write("\r\n");
				writer.flush();
			}
			
			reader.close();
			writer.close();
		}
		catch(Exception ex)
		{
			logger.error(ex, ex);
			logger.error(bracketedTree.toString());
		}
	}
	
	public static void main(String[] args)
	{
		CTBBracketedConvert convert = new CTBBracketedConvert();
		
		//convert.convert(new File("C:/NLP/语料/ctb8.0/data/bracketed/chtb_4320.wb"), "C:/NLP/语料/ctb8.0/data/ctb/");
		convert.convert("C:/NLP/语料/ctb8.0/data/bracketed/", "C:/NLP/语料/ctb8.0/data/ctb/");
		
		
	}
}
